var http = require("../utils/http.js");
var _app;
var _httpValue;
var _httpBackFun;
var _list = [];
var _index = 0;

/**
 * 封装的Lenovo数据请求类
 * 先验证token是否过期，如果过期先请求token再执行方法
 */
function requests(value, backFun) {
  if (_app.dataM.isLoadingUserInfo()) {
    var temp = {};
    temp.value = value;
    temp.fun = backFun;
    _list.push(temp);
  } else {
    _httpValue = value;
    _httpBackFun = backFun;
    //让数据中心准备token信息
    _app.dataM.prepareBaseInfo(back);
  }
}

function requestBase(value, backFun) {
  http.request(value, backFun);
}

function back() {
  if (_httpValue && _httpValue != "") {
    http.request(_httpValue, _httpBackFun);
  }
  _httpValue = "";
  _httpBackFun = null;
  if (_list.length > 0) {
    for (var item in _list) {
      if (item == _index) {
        _index++;
        _httpValue = _list[item]["value"];
        _httpBackFun = _list[item]["fun"];
        _app.dataM.prepareBaseInfo(back);
      }
    }
  }
}

function init(value) {
  if (!_app) {
    _app = value;
    _list = [];
    _index = 0;
    //封装的网络数据请求初始化
    http.init(value);
  }
}

module.exports = {
  init: init,
  request: requests,
  requestBase: requestBase
}